package com.angus.modules.content.mapper;

import com.angus.modules.content.domain.SysOrder;
import com.angus.modules.content.dto.QueryBaseDto;
import com.angus.modules.content.dto.SysFinanceDto;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 订单映射器
 * @author lineCode
 * @date 2025-08-17
 * @description 订单映射器
 */
@Mapper
public interface SysOrderMapper extends BaseMapper<SysOrder> {
        @Select("<script>" +
                "SELECT so.*, u.nick_name " +
                "FROM sys_order so " +
                "LEFT JOIN front_user u ON so.user_id = u.user_id " +
                "WHERE so.del_flag = 0 " +
                "AND u.del_flag = 0 " +
                "<if test='orderNo != null and orderNo != &apos;&apos;'>AND so.order_no = #{orderNo}</if>" +
                "<if test='courseCode != null and courseCode != &apos;&apos;'>AND so.course_code = #{courseCode}</if>" +
                "<if test='courseName != null and courseName != &apos;&apos;'>AND so.course_name LIKE CONCAT('%',#{courseName},'%')</if>" +
                "<if test='userId != null and userId > 0'>AND so.user_id = #{userId}</if>" +
                "<if test='payMethod != null and payMethod != 0'>AND so.pay_method = #{payMethod}</if>" +
                "<if test='status != null and status != 0'>AND so.status = #{status}</if>" +
                "<if test='recommendCode != null'>AND so.recommend_code = #{recommendCode}</if>" +
                "<if test='startTime != null and endTime != null and startTime != &apos;&apos; and endTime != &apos;&apos;'>AND so.create_time BETWEEN #{startTime} AND #{endTime}</if>"+
                "<if test='offset != null and limit != null'>" +
                "    LIMIT #{offset, jdbcType=INTEGER}, #{limit, jdbcType=INTEGER}" +
                "</if>" +
                "</script>"
        )
        List<SysOrder> getOrderList(
                @Param("orderNo") String orderNo,
                @Param("courseCode") String courseCode,
                @Param("courseName") String courseName,
                @Param("userId") Long userId,
                @Param("payMethod") Integer payMethod,
                @Param("status") Integer status,
                @Param("recommendCode") Long recommendCode,
                @Param("startTime") String startTime,
                @Param("endTime") String endTime,
                @Param("offset") Integer offset,  // 新增：偏移量（从0开始）
                @Param("limit") Integer limit     // 新增：每页条数
        );
        /**
         * 订单收益 - 按日期统计
         * @param startTime 开始时间（可选，格式：YYYY-MM-DD）
         * @param endTime 结束时间（可选，格式：YYYY-MM-DD）
         * @param courseName 课程码（可选）
         * @return 按日期统计的订单收益列表
         */
        @Select("<script>" +
                "SELECT " +
                "    DATE_FORMAT(c.pay_time, '%Y-%m-%d') AS day, " +
                "    IFNULL(SUM(c.course_amount), 0) AS course_amount " +
                "FROM sys_order c " +
                "WHERE c.status &gt; 1 " + // 转义 '>'
                "<if test='courseName != null and courseName != &apos;&apos;'>" +
                "    AND c.course_name LIKE CONCAT('%', #{courseName}, '%')" +
                "</if>" +
                "<if test='startTime != null and startTime != &apos;&apos; and endTime != null and endTime != &apos;&apos;'>" +
                "    AND c.create_time BETWEEN STR_TO_DATE(#{startTime}, '%Y-%m-%d') AND STR_TO_DATE(#{endTime}, '%Y-%m-%d')" +
                "</if>" +
                "GROUP BY DATE_FORMAT(c.pay_time, '%Y-%m-%d') " +
                "ORDER BY DATE_FORMAT(c.pay_time, '%Y-%m-%d') DESC" +
                "</script>")
        List<Map<String, Object>> getPayFeesProfitByTime(
                @Param("startTime") String startTime,
                @Param("endTime") String endTime,
                @Param("courseName") String courseName
        );

        /**
         * 获取支付费用列表（多表关联查询）
         * 新增 offset 和 limit 参数接收计算后的分页值
         */
        @Select("<script>" +
                "SELECT " +
                "    o.*, " +
                "    u.nick_name " +
                "FROM sys_order o " +
                "LEFT JOIN front_user u ON o.user_id = u.user_id " +
                "WHERE o.del_flag = 0 " +
                "AND o.status &gt; 1 " + // 转义 '>'
                "<if test='financeDto.courseCode != null and financeDto.courseCode != &apos;&apos;'>" +
                "    AND o.course_code = #{financeDto.courseCode}" +
                "</if>" +
                "<if test='financeDto.courseName != null and financeDto.courseName != &apos;&apos;'>" +
                "    AND o.course_name LIKE CONCAT('%', #{financeDto.courseName}, '%')" +
                "</if>" +
                "<if test='financeDto.courseType != null and financeDto.courseType != &apos;&apos;'>" +
                "    AND o.course_type = #{financeDto.courseType}" +
                "</if>" +
                "<if test='financeDto.payMethod != null and financeDto.payMethod != 0'>" +
                "    AND o.pay_method = #{financeDto.payMethod, jdbcType=INTEGER}" +
                "</if>" +
                "<if test='financeDto.status != null and financeDto.status != 0'>" +
                "    AND o.status = #{financeDto.status, jdbcType=INTEGER}" +
                "</if>" +
                "<if test='financeDto.startTime != null and financeDto.startTime != &apos;&apos; and financeDto.endTime != null and financeDto.endTime != &apos;&apos;'>" +
                "    AND o.create_time BETWEEN STR_TO_DATE(#{financeDto.startTime}, '%Y-%m-%d') AND STR_TO_DATE(#{financeDto.endTime}, '%Y-%m-%d')" +
                "</if>" +
                "<if test='originalDto.orderByField != null and originalDto.orderByField != &apos;&apos;'>" +
                "    ORDER BY " +
                "    <choose>" +
                "        <when test='originalDto.orderByField == &apos;createTime&apos;'>o.create_time</when>" +
                "        <when test='originalDto.orderByField == &apos;updateTime&apos;'>o.update_time</when>" +
                "        <otherwise>o.id</otherwise>" +
                "    </choose>" +
                "    <if test='originalDto.orderByType != null and originalDto.orderByType == &apos;asc&apos;'>ASC</if>" +
                "    <if test='originalDto.orderByType == null or originalDto.orderByType == &apos;desc&apos;'>DESC</if>" +
                "</if>" +
                "<if test='originalDto.orderByField == null or originalDto.orderByField == &apos;&apos;'>" +
                "    ORDER BY o.id DESC" +
                "</if>" +
                "<if test='offset != null and limit != null'>" +
                "    LIMIT #{offset, jdbcType=INTEGER}, #{limit, jdbcType=INTEGER}" +
                "</if>" +
                "</script>")
        List<SysOrder> getPayFeesList(
                @Param("originalDto") QueryBaseDto originalDto,
                @Param("financeDto") SysFinanceDto financeDto,
                @Param("offset") Integer offset,  // 新增：偏移量（从0开始）
                @Param("limit") Integer limit     // 新增：每页条数
        );
}